﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;
using Mimoza.ObjectLayer;
using Mimoza.PluginLayer;

namespace Mimoza.Apps.Console.Commands
{
    public partial class PolicyWizard : Mimoza.GUI.BaseWizard
    {      
        Mimoza.Apps.Console.Application m_application;
        Policy m_policy = null;
        PluginManager m_PluginManager = null;
        IConfigPlugin m_plugin = null;       

        public PolicyWizard(Mimoza.Apps.Console.Application application, Policy policy)
        {
            InitializeComponent();

            m_application = application;
            m_policy = policy;
            m_PluginManager = application.PluginManager;
            m_plugin = application.PluginManager.GetConfigPlugin(policy.PluginID);
            if (null != m_plugin)
            { 
                this.UserData = m_plugin.Deserialize(m_policy.Data);
            }            
        }
        public Application Application
        {
            get 
            {
                return m_application;
            }
        }
        public Policy Policy 
        {
            get 
            {
                return m_policy;
            }
        }
        public PluginManager PluginManager
        {
            get
            {
                return m_PluginManager;
            }
        }
        public IConfigPlugin Plugin 
        {
            get
            {
                return m_plugin;
            }
            set
            {
                m_plugin = value;
                if (m_plugin is PluginLayer.IConfigPluginInitialize) 
                {
                    (m_plugin as PluginLayer.IConfigPluginInitialize).SetObjectLayer(Application.ObjectLayer);
                }

                if ( null != m_plugin && null != m_policy.Data)
                {
                    this.m_policy.PluginID = m_plugin.Identity;
                    this.UserData = m_plugin.Deserialize(m_policy.Data);
                } else {
                    this.m_policy.PluginID = null;
                    this.UserData = null;
                }
            }
        }
 
        override public void Init(WizardMode mode)
        {
            base.Init(mode);

            PolicyWizardGeneralPage general = new PolicyWizardGeneralPage();
            this.AddPage(general);

            if (mode == WizardMode.Wizard)
            {
                PolicyWizardPluginTypePage type = new PolicyWizardPluginTypePage();
                this.AddPage(type);
                this.Text = Resources.Resource.PolicyWizardCreate;
            }
            else 
            {
                this.Text = Resources.Resource.PolicyWizardEdit;
                if (null != m_plugin)
                {
                    foreach (IWizardPage page in m_plugin.Pages)
                    {
                        this.AddPage(page);
                    }
                }
            }
        }
        override public void OnFinish()
        { 
            this.Policy.Data = m_plugin.Serialize(this.UserData);
            
            this.Policy.PluginID = m_plugin.Identity;
            this.Policy.IsHardware = m_plugin.IsHardware;
            this.Policy.IsMultiple = m_plugin.IsMultiple;

            if (m_plugin is PluginLayer.IPluginPolicyNotify)
            {
                (m_plugin as PluginLayer.IPluginPolicyNotify).OnBeforePolicySave(this.Policy);
            }
        }
    }
}
